package com.ai.zuochengyun.phase01.class03;

import java.util.List;

public class Code26_MaxHappy {

    public static class Employee {
        int happy;

        List<Employee> nexts;

        public Employee(int happy, List<Employee> nexts) {
            this.happy = happy;
            this.nexts = nexts;
        }
    }

    public static int maxHappy2(Employee head) {
        Info info = process(head);
        return Math.max(info.yes, info.no);
    }

    private static Info process(Employee x) {
        if (x == null) {
            return new Info(0, 0);
        }

        // 头节点是一定来
        int yes = x.happy;
        int no= 0;
        for (Employee next : x.nexts) {
            Info nextInfo = process(next);
            // 在父节点确定来的情况下，next节点就不能来
            yes += nextInfo.no;
            // 在父节点确定不来的情况下，next节点可以来，也可以不来
            no += Math.max(nextInfo.yes, nextInfo.no);
        }

        return new Info(yes, no);
    }


    public static class Info {
        // 头节点来的情况下的最大收益
        public int yes;

        // 头节点不来的情况下的最大收益
        public int no;

        public Info(int yes, int no) {
            this.yes = yes;
            this.no = no;
        }
    }


}
